package com.we823.cms.controller.manage.sys;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageInfo;
import com.we823.cms.common.web.pageable.PageRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.we823.cms.common.WebConstants;
import com.we823.cms.controller.manage.AbstractManageController;
import com.we823.cms.model.sys.Resource;
import com.we823.cms.repository.service.sys.ResourceService;

@Controller
@RequestMapping(WebConstants.MANAGE_URL+"/resource")
public class ResourceManageController extends AbstractManageController<Resource, Integer> {

	private String baseUrl = WebConstants.MANAGE_URL+"/resource/";
	
	private ResourceService resourceService;
	
	@Autowired
	public ResourceManageController(ResourceService resourceService) {
		super(resourceService);
		this.resourceService=resourceService;
		super.baseUrl = baseUrl;
		super.entityName = "resource";
	}

	@Override
	public PageInfo<Resource> getData(PageRequest pageRequest,
									  HttpServletRequest request, Model model, boolean pageable) {
		PageInfo<Resource> resourcePage ;
		if (pageable) {
			resourcePage = resourceService.findAll(pageRequest);
		}else {
			resourcePage = new PageInfo<>(resourceService.findAll());
		}
		return resourcePage;
	}

	@Override
	public String preAdd(Model model) {
		String result = super.preAdd(model);
		StringBuilder resourceBuilder = new StringBuilder();
		List<Resource> resourceModels = resourceService.findByParentid(0, true);
		buildResourceOptions(resourceBuilder,resourceModels,0);
		
		model.addAttribute("resources",resourceBuilder.toString());
		
		return result;
	}
	
	@Override
	public String preEdit(@RequestParam(required=true,value="id",defaultValue="-1")Integer key, Model model) {
		String result = super.preEdit(key, model);
		StringBuilder resourceBuilder = new StringBuilder();
		List<Resource> resourceModels = resourceService.findByParentid(0, true);
		buildResourceOptions(resourceBuilder,resourceModels,0);
		
		model.addAttribute("resources",resourceBuilder.toString());
		return result;
	}

	private void buildResourceOptions(StringBuilder resourceBuilder,
			List<Resource> resourceModels,int deep) {
		if (resourceModels!=null && resourceModels.size()>0) {
			String option = "<option value='%s'>%s</option>";
			String blankName = "";
			for (Resource resourceModel : resourceModels) {
				blankName = StringUtils.leftPad(resourceModel.getName(), resourceModel.getName().length()+deep*2," >");
				resourceBuilder.append(String.format(option, resourceModel.getId(),blankName));
				List<Resource> subresources = resourceModel.getSubresources();
				if (subresources!=null && subresources.size()>0) {
					buildResourceOptions(resourceBuilder, subresources, deep+1);
				}
			}
		}
		
	}

	@Override
	public void setIndetity() {
		setResourceIdentity("sys:resource");
	}

}
